<?php
$this->load->model('tourism_goods_category_model', 'tourism_goods_category');
$categoryLevel = $this->tourism_goods_category->getCategoryLevel(0);


$categoryIds = $this->tourism_goods_category->getCategoryById($result->category_id);
$ids = explode(',', $categoryIds->row()->parents);
array_pop($ids);
array_shift($ids);
array_push($ids, $result->category_id);
$class_a = $ids[1];
$class_b = $ids[2];
$class_c = $ids[3];

$class_a = isset($class_a) ? (int)$class_a : 0;
$class_b = isset($class_b) ? (int)$class_b : 0;
$class_c = isset($class_c) ? (int)$class_c : 0;
?>
<script type="text/javascript">
    $(document).ready(function () {
        var class_a = <?php echo $class_a;?>;
        var class_b = <?php echo $class_b;?>;
        var class_c = <?php echo $class_c;?>;
        var change_class_b = function(){
            $.ajax({
                url: '<?php echo base_url();?>tourismgoodscategory/select_children/'+$('#class_a').val(),
                type: 'GET',
                dataType: 'json',
                success: function(city_json){
                    var classb = document.getElementById('class_b');
                    classb.options.length = 0;
                    classb.options[0] = new Option('请选择二级分类', '');
                    for (var i=0; i<city_json.length; i++) {
                        var len = classb.length;
                        classb.options[len] = new Option(city_json[i].name, city_json[i].id);
                        if (classb.options[len].value == class_b) {
                            classb.options[len].selected = true;
                        }
                    }
                    change_class_c();//重置地区
                }
            });
        }

        change_class_b();//初始化分类

        $('#class_a').change(function(){
            change_class_b();
        });

        var change_class_c = function(){
            $.ajax({
                url: '<?php echo base_url()?>tourismgoodscategory/select_children/'+$('#class_b').val(),
                type: 'GET',
                dataType: 'json',
                success: function(district_json){
                    var classc = document.getElementById('class_c');
                    classc.options.length = 0;
                    classc.options[0] = new Option('请选择三级分类', '');
                    for (var i=0; i<district_json.length; i++) {
                        var len = classc.length;
                        classc.options[len] = new Option(district_json[i].name, district_json[i].id);
                        if (classc.options[len].value == class_c){
                            classc.options[len].selected = true;
                        }
                    }
                }
            });
        }

        $('#class_b').change(function(){
            change_class_c();
        });

        /*初始化 改变3级分类 ajax变换分类id和名称*/
        $(function () {
            var class_c_id = $('input[name="category_id"]').val();
            var goods_attr_id = $('input[name="goods_attr_id"]').val();
            $.ajax({
                url: '<?php echo base_url()?>tourismgoods/select_category_name/' + class_c_id,
                type: 'GET',
                dataType: 'json',
                success: function (data) {
                    if (data.status) {
                        $('input[name="category_id"]').val(data.content.category_id);
                        $('input[name="category_name"]').val(data.content.category_name);
                    }
                }
            });
        });

        /*改变3级分类 ajax变换分类id和名称*/
        $('#class_c').change(function () {
            var class_c_id = $('#class_c option:selected').val();
            var goods_attr_id = $('input[name="goods_attr_id"]').val();
            $.ajax({
                url: '<?php echo base_url()?>tourismgoods/select_category_name/' + class_c_id,
                type: 'GET',
                dataType: 'json',
                success: function (data) {
                    if (data.status) {
                            $('input[name="category_id"]').val(data.content.category_id);
                        $('input[name="category_name"]').val(data.content.category_name);
                    }
                }
            });
//
        });

        /*修改按钮*/
        var disabled_class = function () {
            $('#class_a, #class_b, #class_c').attr('disabled', true);
            $('.category_select_modify').on('click', '', function () {
                if(confirm("确定要修改吗？"))
                {
                    $('#class_a, #class_b, #class_c').attr('disabled', false);
                }
            })
        }
        disabled_class();

    });
</script>

<select id="class_a" class="required" name="class_a" style="width:150px;">
    <?php foreach ( $categoryLevel as $key => $item ): ?>
        <option value="<?php echo $item['id']; ?>"
                <?php if ($item['id'] == $class_a): ?>selected="selected"<?php endif; ?> ><?php echo $item['name']; ?></option>
    <?php endforeach; ?>
</select>

<select id="class_b" class="required" name="class_b" style="width:150px;">
    <option value="">请选择二级分类</option>
</select>

<select id="class_c" class="required" name="class_c" style="width:150px;">
    <option value="">请选择三级分类</option>
</select>
<button type="button" class="category_select_modify btn blue mini">修改</button>
